package gov.mogaha.ntis.cmm.mob;

import gov.mogaha.ntis.cmm.mob.adapter.CmmMobApvCnAdapter;
import gov.mogaha.ntis.cmm.mob.baseactivity.BaseActivity;
import gov.mogaha.ntis.cmm.mob.dto.CmmMobApvCnDto;
import gov.mogaha.ntis.cmm.mob.servlet.CmmMobServlet;
import gov.mogaha.ntis.cmm.mob.servlet.CmmMobServletHandler;
import gov.mogaha.ntis.cmm.mob.servlet.CmmMobServlet.DataSet;
import gov.mogaha.ntis.cmm.mob.servlet.CmmMobServletHandler.HttpResult;
import gov.mogaha.ntis.cmm.mob.util.Toaster;
import gov.mogaha.ntis.cmm.mob.view.EditTextView;
import gov.mogaha.ntis.cmm.mob.view.TitleView;

import java.util.ArrayList;
import java.util.Hashtable;

import wire.proto.Binding;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class CmmMobApvCn extends BaseActivity implements OnClickListener, AdapterView.OnItemClickListener {
	
	public static String upmuTag = "CmmMobApvCn";
	public static String widlUrl = "mobile/RTP_MOB_CMM";
	public static String actionName = "selectCmmMobApvCn";
	
	private EditTextView upchNm;				//업소명
	private EditTextView apvPermNo;			//인허가번호
	
	//인허가정보 총갯수
	private TextView listCountText;
	//리스트
	ListView srchList;
	
	private CmmMobApvCnAdapter cmmMobApvCnAdapter;
	private final ArrayList<CmmMobApvCnDto> srchDataList;

	private String dispoRaiseWrkCode;
	private String upchCobCode;
	
	public CmmMobApvCn() {
		srchDataList = new ArrayList<CmmMobApvCnDto>();
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.cmmmobapvcn);
		
		savedInstanceState = this.getIntent().getExtras();
		
		if(savedInstanceState != null) {
			this.dispoRaiseWrkCode = savedInstanceState.getString("dispo_raise_wrk_code");
			this.upchCobCode = savedInstanceState.getString("upch_cob_code");
		}
		
		TitleView titleView = (TitleView)findViewById(R.id.title_view);
		titleView.setTitle("인허가 검색");
		titleView.setBackgroundImage(R.drawable.titlebarsub);
		
		upchNm = (EditTextView)findViewById(R.id.edit_ApvCn_Name);		//업소명
		apvPermNo = (EditTextView)findViewById(R.id.edit_ApvCn_Num);	//인허가번호
		
		//버튼
		Button apvCnSearch = (Button)findViewById(R.id.ApvCn_Search);
		apvCnSearch.setOnClickListener(this);
		Button apvCnCancel = (Button)findViewById(R.id.ApvCn_Cancel);
		apvCnCancel.setOnClickListener(this);
		
		listCountText = (TextView)findViewById(R.id.list_count_text);
		//List
		srchList = (ListView)findViewById(R.id.srch_list);	//리스트
		srchList.setOnItemClickListener(this);
	}
	
	public void getSrchDataList(){
		
		Binding inputs = new Binding();
		
		inputs.put("dispo_raise_wrk_code", dispoRaiseWrkCode);
		inputs.put("upch_cob_code", this.upchCobCode);
		inputs.put("resi_orgz_nm", this.upchNm.getText().toString());
		inputs.put("apv_perm_reg_mgt_no", this.apvPermNo.getText().toString());
		
		//통신핸들러 선언
		CmmMobServletHandler handler = new CmmMobServletHandler(this);
		
		CmmMobServlet httpThread = new CmmMobServlet(this, widlUrl, upmuTag, actionName, 
				handler.getHandler(new HttpResult() {
					@Override
					public void httpCallback(DataSet data) {
						ArrayList<Hashtable<String, String>> srchData = new ArrayList<Hashtable<String, String>>();
						
						srchData = data.getHashData();
						
						srchDataList.clear();
						
						for(Hashtable<String, String> tmp : srchData) {
							CmmMobApvCnDto dto = new CmmMobApvCnDto();
							
							dto.mTgtApvNo = tmp.get("tgt_apv_no");
							dto.mUpchNm = tmp.get("upch_nm");
							dto.mUpchTelno = tmp.get("upch_telno");
							dto.mUpchAdmdngCode = tmp.get("upch_admdng_code");
							dto.mUpchRegnCode = tmp.get("upch_regn_code");
							dto.mUpchSan = tmp.get("upch_san");
							dto.mUpchBunji = tmp.get("upch_bunji");
							dto.mUpchHo = tmp.get("upch_ho");
							dto.mUpchSpecAddr = tmp.get("upch_spec_addr");
							dto.mUpchSpecDng = tmp.get("upch_spec_dng");
							dto.mUpchSpecHo = tmp.get("upch_spec_ho");
							dto.mUpchTong = tmp.get("upch_tong");
							dto.mUpchBan = tmp.get("upch_ban");
							dto.mUpchAddr = tmp.get("upch_addr");
							
							srchDataList.add(dto);
						}
						
						listCountText.setText("총 "+srchDataList.size()+"건의 검색결과");
						
						if(srchDataList.size() == 0) {
							Toaster.showMsg(CmmMobApvCn.this, R.string.no_data_trgt);
							
						} else {
							cmmMobApvCnAdapter = new CmmMobApvCnAdapter(CmmMobApvCn.this, srchDataList);
							srchList.setAdapter(cmmMobApvCnAdapter);
							
							upchNm.setText("");
							apvPermNo.setText("");
						}
					}
				}), 
				inputs);
		httpThread.start();
	}
	
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

		CmmMobApvCnDto apvData = srchDataList.get(position);

		Intent intent = new Intent();
		intent.putExtra("apv_cn", apvData);
		this.setResult(Activity.RESULT_OK, intent);
		this.finish();
		this.overridePendingTransition(0, 0);
	}
	
	@Override
	public void onClick(View view) {
		InputMethodManager inputMethodManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
		
		switch (view.getId()) {
		case R.id.ApvCn_Search :
			//검색버튼을 눌렀을 때의 작업
			if(this.upchNm.getText().length() > 0 && this.upchNm.getText().length() < 2) {
				showMsg(R.string.pmh_find_regdata_name_short);
				return;
			} 
			inputMethodManager.hideSoftInputFromWindow(this.upchNm.getWindowToken(), 0);
			this.getSrchDataList();
			break;
			
		case R.id.ApvCn_Cancel :
			//취소버튼을 눌렀을 때의 작업
			inputMethodManager.hideSoftInputFromWindow(this.upchNm.getWindowToken(), 0);
			Intent intent = getIntent();
			this.setResult(Activity.RESULT_CANCELED, intent);
			this.finish();
			this.overridePendingTransition(0, 0);
			break;
		default:
			break;
		}
	}
}


















